﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.Admin;

public partial class Admin_adminLevelManageMent : System.Web.UI.Page
{
    /// <summary>
    /// 后台用户级别管理
    /// </summary>
    private static string AID = "";
    //用于刷新的方法
    private void bindGrid()
    {
        try
        {
            AspNetPager1.RecordCount = Convert.ToInt32(adminLevelService.S_selectAllAdminLevel(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex)[1].Rows[0][0].ToString().Trim());
            GVAdminLevel.DataSource = adminLevelService.S_selectAllAdminLevel(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex)[0];
            GVAdminLevel.DataBind();
            update.Visible = false;
            lblMessages.Visible = false;
            bntInsert.Visible = false;
        }
        catch (Exception ex)
        {
            ClientScriptManager csm = this.ClientScript;
            csm.RegisterStartupScript(this.GetType(), "admin", "alter('" + ex.Message.ToString().Trim() + "')", true);
        }
    }

    //加载的时候执行
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"].ToString().Trim() != "1")
        {
            Response.Redirect("Login.aspx");
        }
        bntInsert.Visible = false;
        if (!IsPostBack)
        {
            bindGrid();
        }
    }

    //分页控件的刷新方法
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindGrid();
    }

    //点修改的时候
    protected void GVAdmin_SelectedIndexChanged(object sender, EventArgs e)
    {
        update.Visible = true;
        bntInsert.Visible = false;
        btnUpdate.Visible = true;
        try
        {
            txtName.Text = GVAdminLevel.SelectedRow.Cells[1].Text.ToString().Trim();
            txtPwd.Text = GVAdminLevel.SelectedRow.Cells[2].Text.ToString().Trim();
            AID = GVAdminLevel.SelectedRow.Cells[0].Text.ToString().Trim();
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //删除
    protected void GVAdmin_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            lblMessages.Visible = false;
            if (AdminServices.SP_delete("AdminLevel", "ALID=" + GVAdminLevel.Rows[e.RowIndex].Cells[0].Text.ToString().Trim()))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "删除成功";
                bindGrid();
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "删除失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }


    //修改
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            string strSql = "update AdminLevel set  "
                + "ALNAame='" + txtName.Text.ToString().Trim() + "',ALText='" + txtPwd.Text.ToString().Trim() + "'  where  ALID=" + Convert.ToInt32(AID.ToString().Trim());
            if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "修改成功";
                bindGrid();
                update.Visible = false;
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "修改失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //添加新用户时
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        try
        {
            txtName.Text = "";
            txtPwd.Text = "";
            update.Visible = true;
            btnUpdate.Visible = false;
            bntInsert.Visible = true;
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //添加
    protected void bntInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string strSql = "insert into  AdminLevel(ALNAame,ALText)values('" + txtName.Text.ToString().Trim() + "','" + txtPwd.Text.ToString().Trim() + "')";
            if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "添加成功";
                bindGrid();
                update.Visible = false;
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "添加失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
}